/**
 * Crear un programa que permita al usuario introducir los códigos RGB 
 * en tres cuadros de texto diferentes y cuando él o ella hace clic en 
 * el botón de color de la pantalla, el color de fondo del formulario 
 * cambiará de acuerdo con el código RGB.
 */
package com.virglinio.rgb;
/*
 *  Principal.java is part of rgb
 *
 *  rgb is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  rgb is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with rgb.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * 
 * @author Mario Fernando Virglinio
 * @see <a href="http://www.virglinio.com.ar">http://www.virglinio.com.ar</a>
 * 
 */
public class Principal {

	private JFrame frmRgb;
	private JTextField txtR;
	private JTextField txtG;
	private JTextField txtB;
	
	private Color colorFondoPanelContenedorPorDefecto;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Principal window = new Principal();
					window.frmRgb.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Principal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmRgb = new JFrame();
		frmRgb.setTitle("RGB");
		frmRgb.setBounds(100, 100, 582, 235);
		frmRgb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmRgb.getContentPane().setLayout(null);
		colorFondoPanelContenedorPorDefecto = frmRgb.getContentPane().getBackground();
		
		JLabel lblEntrarCdigoRgb = new JLabel("Entrar Código RGB (Por favor ingrese un número entre 0 y 255 en cada caja)");
		lblEntrarCdigoRgb.setBounds(12, 12, 540, 15);
		frmRgb.getContentPane().add(lblEntrarCdigoRgb);
		
		txtR = new JTextField();
		txtR.setBounds(50, 72, 114, 19);
		frmRgb.getContentPane().add(txtR);
		txtR.setColumns(10);
		
		txtG = new JTextField();
		txtG.setBounds(214, 72, 114, 19);
		frmRgb.getContentPane().add(txtG);
		txtG.setColumns(10);
		
		txtB = new JTextField();
		txtB.setBounds(389, 72, 114, 19);
		frmRgb.getContentPane().add(txtB);
		txtB.setColumns(10);
		
		JLabel lblR = new JLabel("R");
		lblR.setBounds(96, 45, 70, 15);
		frmRgb.getContentPane().add(lblR);
		
		JLabel lblG = new JLabel("G");
		lblG.setBounds(258, 45, 70, 15);
		frmRgb.getContentPane().add(lblG);
		
		JLabel lblB = new JLabel("B");
		lblB.setBounds(435, 45, 70, 15);
		frmRgb.getContentPane().add(lblB);
		
		JButton btnCambiarColor = new JButton("Cambiar color");
		btnCambiarColor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int r = Integer.parseInt(txtR.getText());
				int g = Integer.parseInt(txtG.getText());
				int b = Integer.parseInt(txtB.getText());
				frmRgb.getContentPane().setBackground(new Color(r, g, b));          
			}
		});
		btnCambiarColor.setBounds(50, 141, 156, 25);
		frmRgb.getContentPane().add(btnCambiarColor);
		
		JButton btnVolver = new JButton("Volver");
		btnVolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frmRgb.getContentPane().setBackground(colorFondoPanelContenedorPorDefecto);
			}
		});
		btnVolver.setBounds(339, 141, 117, 25);
		frmRgb.getContentPane().add(btnVolver);
	}
}
